package cheme.ProcessEditor.Figures.Util;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * A customized Label based on the label used in the flow example. 
 * Primary selection is denoted by highlight and focus rectangle. 
 * Normal selection is denoted by highlight only. Borrowed from the Flow Editor example
 * @author Daniel Lee
 */
public class EditableLabel extends Label
{
//	private String emptyText = "";
//	Color emptyForegroundColor = ChemePresentation.Fields.initialForeground;
//	Color nonemptyForegroundColor = ChemePresentation.Fields.defaultForeground;
	
	private boolean selected;
	
	public EditableLabel(String text)
	{
		super(text);
		setFocusTraversable(true);
	}
	
//	public void setEmptyText(String text)
//	{
//		emptyText = text;
//	}
	
//	@Override
//	public void setForegroundColor(Color fg)
//	{
//		nonemptyForegroundColor = fg;
//		super.setForegroundColor(fg);
//	}
	
	private Rectangle getSelectionRectangle()
	{
		Rectangle bounds = getTextBounds().getCopy();
		bounds.expand(new Insets(2, 2, 0, 0));
		translateToParent(bounds);
		bounds.intersect(getBounds());
		return bounds;
	}
	
	/**
	 * paints figure differently depends on the whether the figure has focus or is selected 
	 */
	protected void paintFigure(Graphics graphics)
	{
		graphics.pushState();
		
		if (selected)
		{
			graphics.setBackgroundColor(ColorConstants.menuBackgroundSelected);
			graphics.fillRectangle(getSelectionRectangle());
			graphics.setForegroundColor(ColorConstants.white);	
		}
		
		super.paintFigure(graphics);

		graphics.popState();
	}

	/**
	 * Sets the selection state of this SimpleActivityLabel
	 * 
	 * @param b
	 *            true will cause the label to appear selected
	 */
	public void setSelected(boolean b)
	{
		selected = b;
		repaint();
	}
	
	@Override
	public String toString()
	{
		return getText();
	}
	
//	@Override
//	public void setText(String s)
//	{		
//		if(null == s || s.isEmpty())
//		{
//			super.setForegroundColor(emptyForegroundColor);
//			super.setText(emptyText);
//		}
//		else
//		{
//			super.setForegroundColor(nonemptyForegroundColor);
//			super.setText(s);
//		}
//	}
}

